package com.iwuke.dance.util;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

/**
 * @author: 魏从响
 * @date: 2025/9/15
 */
public class PhoneMaskUtil {

    /**
     * 判断支付密码是否包含连续三位相同数字
     * @param password 支付密码
     * @return true-包含连续三位相同数字 false-不包含
     */
    public static boolean hasThreeSameDigits(String password) {
        // 使用正则表达式匹配连续三位相同数字
        return password.matches(".*(\\d)\\1{2}.*");
    }


    // 手机号脱敏核心方法
    public static String maskPhoneNumber(String phone) {
        if (phone == null || phone.length() != 11) return phone;
        return phone.substring(0, 3) + "****" + phone.substring(7);
    }

    // EditText 实时脱敏输入监听器
    public static void applyPhoneMask(EditText editText) {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String current = s.toString().replaceAll("\\D", "");
                if (current.length() > 11) {
                    current = current.substring(0, 11);
                }

                if (current.length() == 11) {
                    editText.removeTextChangedListener(this);
                    String masked = maskPhoneNumber(current);
                    editText.setText(masked);
                    editText.setSelection(masked.length());
                    editText.addTextChangedListener(this);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {}
        });
    }
}
